const app = getApp();
const call = require('../../../../utils/request.js')
Page({
  data: {
    actionSheet: false,
    position: '',
    proInfo: {}
  },
  //加入购物车
  onShoppingCart() {
    let that = this;
    wx.getStorage({ //查询储购物车
      key: 'shoppingCart',
      success(res) {
        //获取本地购物车列表
        const shopList_local = res.data.shoppingList.slice();
        //获取购物车所有店铺列表
        const storeList = [];
        for (var i = 0; i < shopList_local.length; i++) {
          storeList.push(shopList_local[i].shopName)
        }
        //根据 storeList 对商品进行判断，已有店铺增加商品，新的店铺增加店铺并添加商品
        if (storeList.indexOf(that.data.proInfo.shopName) > -1) {
          console.log('已有店铺', res.data.totle);
          const spList = shopList_local.find(i => i.shopName === that.data.proInfo.shopName).data;
          const pushList = {
            proUrl: that.data.proInfo.proUrl,
            classtitle: that.data.proInfo.proName,
            index: spList.length,
            state: 0,
            checked: false,
            describe: that.data.proInfo.proDescribe,
            price: that.data.proInfo.proPrice
          };
          spList.push(pushList); //加入商品
        } else {
          console.log('新的店铺', res.data.totle);
          shopList_local.push({
            shopName: that.data.proInfo.shopName,
            data: [{
              proUrl: that.data.proInfo.proUrl,
              classtitle: that.data.proInfo.proName,
              index: 0,
              state: 0,
              checked: false,
              describe: that.data.proInfo.proDescribe,
              price: that.data.proInfo.proPrice
            }],
            checkeedAll: false
          });
        }
        //把计算好的购物车存入本地存储
        wx.setStorage({
          key: 'shoppingCart',
          data: {
            totle: res.data.totle + 1,
            shoppingList: shopList_local
          },
        });
        //提示信息
        wx.showToast({
          title: '已加入购物车~',
          icon: 'success',
          duration: 2000
        });
      },
      fail(err) {
        console.log('初始化购物车', err);
        //生成初始购物车列表
        // var shoppingList = [{
        //   shopName: that.data.proInfo.shopName,
        //   data: [that.data.proInfo],
        //   checked: false
        // }]
        var shoppingList = [{
          shopName: that.data.proInfo.shopName,
          data: [{
            proUrl: that.data.proInfo.proUrl,
            classtitle: that.data.proInfo.proName,
            index: 0,
            state: 0,
            checked: false,
            describe: that.data.proInfo.proDescribe,
            price: that.data.proInfo.proPrice
          }],
          checkeedAll: false
        }]

        //把shoppingList作为初始购物车存入本地存储
        wx.setStorage({
          key: 'shoppingCart',
          data: {
            totle: 1,
            shoppingList: shoppingList
          },
        })
        //提示信息
        wx.showToast({
          title: '已加入购物车~',
          icon: 'success',
          duration: 2000
        });
      }
    })
  },
  onSettlement() {
    if (this.data.productId === '152158') {
      wx.navigateTo({
        url: `/pages/shop/order/settlement/settlement`,
      })
    } else {
      wx.showToast({
        title: '该商品不是测试商品哦',
        icon: 'none'
      })
    }
  },
  onLoad(option) {
    let that = this;
    console.log('获取页面传递的参数', option);
    this.setData({
      productId: option.productId
    })
    const {
      province,
      city,
      district
    } = app.globalData.userPosition
    console.log('定位信息', province, city, district)
    that.setData({
      position: province + city + district
    })
    //获取本地存储的列表信息
    wx.getStorage({
      key: 'detail',
      success(res) {
        console.log(res.data);
        const pro = res.data[0]
        wx.setNavigationBarTitle({
          title: pro.name
        })
        that.setData({
          proInfo: {
            proName: pro.name,
            proUrlGroup: [pro.image, pro.image, pro.image],
            proDescribe: pro.info || '暂无介绍',
            proPrice: pro.price,
            proUrl: pro.icon,
            shopName: pro.shopName
          }
        })
      },
      fail() {
        console.log('打开页面失败，获取不到本地存储信息')
      }
    })
  },
  onShow() {
    console.log('onShow')
  },
  onUnload() {
    console.log('onUnload');
    wx.removeStorage({
      key: 'detail',
      success(res) {
        console.log(res)
      }
    })
  }
})